var listObj = (function ($) {
	var sourceData;
	var pageType = '', pageData = {},statusKey=''
	var start = 0,Limit = 20;
	var pullDom;
	var pulling = false;
	var nurseId='',perInfo='',perId=''
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getZkData(){
		if (pulling) {
			pullDom.endPullUpToRefresh()
			return;
		}
		pulling = true
		var stDate = $('#stBtn')[0].innerText
		var endDate = $('#endBtn')[0].innerText
		//parr 病区^活动形式^参与形式^级别^开始日期^结束日期^状态
		//规范参与备案:类型^制定/修订^专业组^开始日期^结束日期^状态
		var pdict = {'start':start,'limit':Limit,'parr':'^^^^'+stDate+'^'+endDate+'^','nurseid':nurseId}
		if (pageType == '规范参与备案') {
			pdict['parr'] ='^^^'+stDate+'^'+endDate+'^' 
		}
		var paramDict = {'method':pageData['method'],'params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict)+pageData['method'])
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				pullDom.endPullUpToRefresh();
				pulling = false
				return;
			}
			
			if (start == 0) {
				sourceData = dataArr;
			}else{
				sourceData = sourceData.concat(dataArr)
			}
			updateUI(dataArr)
			if (dataArr.length < Limit) {
				pullDom.endPullUpToRefresh(true);
			}else{
				pullDom.endPullUpToRefresh();
			}
			start += dataArr.length;
			pulling = false
		}, function(errorStr) {
		 	pullDom.endPullUpToRefresh();
		 	pulling = false
		});
	}
	function updateUI(dataArr){
		var titleStr = pageData['titleStr']
		var icon = pageData['icon']
		var UL = $('#UL')[0];
		if (start == 0) {
			UL.innerHTML = ''
		}
		var oldCount = UL.querySelectorAll('li').length;
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var statusText = oneData[statusKey]
			var statusIcon = ''
			if (statusText == '保存') {
				statusIcon = 'nur-statusS'
			}else if (statusText == '提交') {
				statusIcon = 'nur-statusR'
			}else if (statusText == '审核') {
				statusIcon = 'nur-statusA'
			}else if (statusText == '驳回'){
				statusIcon = 'nur-statusB'
			}
			var html = '<div class="liheader">'+
					'<img class="headerImg" src="../../dependence/wkplatform/images/'+icon+'.png"/>'+
					'<div class="headerText mui-ellipsis">'+oneData['CreatorName']+' '+titleStr+'</div>'+
					'<div class="headerTime">'+oneData['CreateDate']+'</div>'+
				'</div>'+getZKhtml(oneData)+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'
			
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			newLi.id = 'li-'+(oldCount+i);
			UL.appendChild(newLi)
			newLi.addEventListener('tap',tableCellTapped)
		}
	}
	function tableCellTapped(){
		var indexStr = this.id.split('-')[1]
		var index = parseInt(indexStr)
		var currData = sourceData[index]
		if (currData[statusKey] == '保存'&&nurseId == currData['Creator']) {
			var pageName = getZkNewPageName(pageType)
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageType':pageType,'data':currData,'perInfo':perInfo});
			webview.show('fade-in');
			return
		}
		var keys = {'titleStr':pageData['titleStr'],'pageArr':pageData['pageArr'],'applyer':'CreatorName','applyDate':'CreateDate','detailM':pageData.detailM}
		var webview = plus.webview.create('zkDetail.html', 'zkDetail', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'keys':keys,'data':currData,'pageType':pageType,'statusKey':statusKey});
		webview.show('fade-in');
	}

	//发表论文备案
	function getZKhtml(dict){
		var htmls = ''
		var pageArr = pageData['pageArr']
		for (var i = 0; i < pageArr.length;i++) {
			var oneData = pageArr[i];
			var title = oneData['title']
			var keyStr = oneData['key']
			var keyType = oneData['type']
			var valueStr = dict[keyStr];
			var html = '<div class="oneline"><div class="leftText">'+title+'：</div><div class="rightText mui-ellipsis">'
			if (keyType == 'auth') {
				html += getAuthorHtml(valueStr)
			}else if (keyType == 'file') {
				html +='<div class="nurfont nur-fujian"></div>'
				html +=getFileHtml(valueStr)
			}else{
				html += valueStr
			}
			html +='</div></div>'
			htmls += html
		}
		return htmls
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		perInfo = app.getState()
		nurseId=perInfo['loginID']
		perId = perInfo['loginPerID']
		var stBtn = $('#stBtn')[0]
		var endBtn = $('#endBtn')[0]
		var curMonth = getDeviceYearMonthStr();
		stBtn.innerText = curMonth +'-01'
		var currDict = getYMDWithStr(curMonth)
		endBtn.innerText = curMonth +'-' + getMonthMaxDay(currDict['year'], currDict['month'])
		stBtn.addEventListener('tap',dateBtnTapped)
		endBtn.addEventListener('tap',dateBtnTapped)
		var self = plus.webview.currentWebview()
		pageType = self.title
		$('.mui-title')[0].innerText = pageType
		pageData = getPageInitData(pageType)
		statusKey = getZkStatusKey(pageType)
		var scroll = document.getElementById('scroll');
		pullDom = $(scroll).pullToRefresh({
			up: {
				callback: function() {
					getZkData()
				}
			}
		});
		getZkData();
		//统计按钮
		mui('body').on('tap','#statBtn',function(){
			var webview = plus.webview.create('../platformStat.html', 'platformStat', {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageType':pageType,'title':pageData['titleStr'].replace('备案',''),'nurseid':nurseId,perId:perId});
			webview.show('fade-in');
		})
		//新建按钮
		mui('body').on('tap','#addBtn',function(){
			var pageName = getZkNewPageName(pageType)
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageType':pageType,'perInfo':perInfo});
			webview.show('fade-in');
		})
		
	}
	function dateBtnTapped(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.innerText = dateStr;
			resetZkRefresh();
		}, function(e) {}, {});
	}
	var resetZkRefresh = function (){
		start = 0;
		sourceData = []
		 $('#UL')[0].innerHTML = '';
		 $('#wrapper').scroll().scrollTo(0,0,100);
		if (pullDom) {
			pullDom.refresh(true)
		}
		getZkData();
	}
	var exportObj = {}
	exportObj.resetZkRefresh = resetZkRefresh
	return exportObj
})(mui)

function detailChanged(){
	listObj.resetZkRefresh();
}

/* 专科活动备案
	{"ActivityDate":"2022-07-19","ActivityFile":"","ActivityLevel":"H","ActivityLevelDesc":"护理部","ActivityLocation":"就是地点",
	"ActivityNurse":"2","ActivityNurseName":"张芮菲","ActivityParType":"Z","ActivityParTypeDesc":"组织","ActivityStatus":"Y",
	"ActivityStatusDesc":"提交","ActivityTopic":"就是主题","ActivityType":"L","ActivityTypeDesc":"主题论坛","ActivityWard":"1",
	"ActivityWardDesc":"CCU","AuditDate":"","AuditOpinion":"","AuditTime":"","Auditor":"","AuditorName":"","AuditorWard":"",
	"CreateDate":"2022-07-19","CreateTime":"08:50:56","Creator":"0","CreatorName":"管理员","CreatorWard":"","rw":"7"}
课题专案备案
	{"AuditDate":"2022-02-26","AuditOpinion":"","AuditTime":"11:04:16","Auditor":"0","AuditorName":"管理员","AuditorWard":"",
	"CaseEndDate":"2022-02-26","CaseLevel":"W","CaseLevelDesc":"病区","CaseNurse":"1,2,3","CaseNurseName":"冯亮,张芮菲,周渔",
	"CaseParType":"H","CaseParTypeDesc":"主持","CaseStDate":"2022-02-26","CaseStatus":"A","CaseStatusDesc":"审核",
	"CaseTopic":"课题主题","CaseType":"K","CaseTypeDesc":"课题","CaseWard":"1","CaseWardDesc":"CCU","CaseWork":"1",
	"CreateDate":"2022-02-26","CreateTime":"11:02:47","Creator":"0","CreatorName":"管理员","CreatorWard":"","rw":"3"}
规范参与备案
	{"AuditDate":"","AuditOpinion":"","AuditTime":"","Auditor":"","AuditorName":"","AuditorWard":"","CreateDate":"2022-01-18",
	"CreateTime":"17:34:41","Creator":"0","CreatorName":"管理员","CreatorWard":"","StandardDesc":"cs","StandardFile":"",
	"StandardGroup":"52__1","StandardGroupDesc":"医技组","StandardParticipant":"1,2","StandardParticipantName":"冯亮,张芮菲",
	"StandardStatus":"N","StandardStatusDesc":"保存","StandardType":"S","StandardTypeDesc":"标准","StandardTypeSub":"C",
	"StandardTypeSubDesc":"制定","rw":"4"}
	

*/